//
//  StatusRowViewModel.swift
//  weibo
//
//  Created by cmh on 15/9/6.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit

// 转发微博的可重用标识
let StatusForwardCellID = "StatusForwardCellID"
// 原创微博的可重用标识
let StatusNormalCellID = "StatusNormalCellID"

class StatusRowViewModel: NSObject {
    
    // 微博对象
    var status: Status
    // 行高属性
    var rowHeight: CGFloat = 0
    // 用户头像URL
    var userIconUrl: NSURL? {
        return NSURL(string: status.user?.profile_image_url ?? "")
    }
    /// 配图缩略图 URL 数组
    var thumbnailURLs: [NSURL]?
    
     /// 中等图片数组
    var bmiddleURLs: [NSURL]? {
      
        guard let urls = thumbnailURLs else {
            return nil
        }
        
        var array = [NSURL]()
        
        for url in urls {
        
            let urlString = url.absoluteString.stringByReplacingOccurrencesOfString("/thumbnail/", withString: "/bmiddle/")
            
            array.append(NSURL(string: urlString)!)
        
        }
        
        return array
    }
    
    // 标记是原创还是转载的微博
    var cellID: String {
    
        return status.retweeted_status != nil ? StatusForwardCellID : StatusNormalCellID
    }
    
    // 被转发微博的文字
    var forwordText: String? {
        let username = status.retweeted_status?.user?.name ?? ""
        let text = status.retweeted_status?.text ?? ""
        
        return "@\(username):\(text)"

    }
    
    // 认证类型图片
    var userVerifiedImage: UIImage? {
        switch (status.user?.verified ?? -1) {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }

    // 会员等级
    var userMemberImage: UIImage? {
        
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        
        return nil
        
    }
    
    //  初始化方法
    init(status: Status) {
    
        self.status = status
        
        // 对转发微博和原创微博的图片做处理
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
        
            thumbnailURLs = [NSURL]()
            
            // 遍历数组，插入URL
            for dict in urls {
                
                thumbnailURLs?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
        
        }
        
        super.init()
    }

}
